/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.click.app.webapp.action;

import com.click.app.model.Catalog;
import com.click.app.model.Category;
import com.click.app.model.LabelValue;
import com.click.app.model.Product;
import com.click.app.service.CatalogManager;
import com.click.app.service.GenericManager;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 *
 * @author orgale
 */
public class CatalogAction extends BaseAction {

    private CatalogManager catalogManager;
    private GenericManager<Product, Long> productManager;
    private GenericManager<Category, Long> categoryManager;
    private List catalogs;
    private Catalog catalog;
    private Long id;
    private String name;
    private Set<String> categoryOpions;
    private Set<String> categoryOptionsPicked;
    private List<LabelValue> categoryListLeft;
    private List<LabelValue> categoryListRight;

    public void setId(Long id) {
        this.id = id;
    }

    public Catalog getCatalog() {
        return catalog;
    }

    public void setCatalog(Catalog catalog) {
        this.catalog = catalog;
    }

    public String catalogActual() {
        Catalog newActual = catalogManager.get(id);
        for (Catalog c : catalogManager.getAll()) {
            if (c.isActual()) {
                c.setActual(false);
                c = catalogManager.save(c);
            }
            newActual.setActual(true);
            newActual = catalogManager.save(newActual);
        }
        return SUCCESS;
    }

    public String delete() {
        //   try {
        catalogManager.remove(new Long(catalog.getId()));
        saveMessage(getText("catalog.deleted"));
        return SUCCESS;
    // } catch (Exception e) {
    //   errorMessage(getText("generalError"));
    // return SUCCESS;

    //}
    }

    public List<LabelValue> getCategorysNamesAndValues() {
        List<LabelValue> namesAndValues = new ArrayList<LabelValue>();
        List<Category> cs = categoryManager.getAll();

        for (Category c : cs) {
            namesAndValues.add(new LabelValue(c.getNameLang(), c.getId().toString()));
        }
        return namesAndValues;

    }

    public String edit() {
        System.out.println("editing...");
        if (id != null) {
            catalog = catalogManager.get(id);
            System.out.println("numberofcategories: " + catalog.getCategorys());
            categoryListLeft = getCategorysNamesAndValues();
            categoryListRight = new ArrayList<LabelValue>();
            for (Category c : catalog.getCategorys()) {
                categoryListRight.add(new LabelValue(c.getNameLang(), c.getId().toString()));
                categoryListLeft.remove(getPos(getCategorysNamesAndValues(), c));
            }
            getSession().setAttribute("categorys", catalog.getCategorys());
        } else {
            categoryListLeft = getCategorysNamesAndValues();
            categoryListRight = new ArrayList<LabelValue>();
            catalog = new Catalog();
        }

        return SUCCESS;
    }

    public LabelValue getPos(List<LabelValue> lp, Category cr) {
        for (LabelValue lv : lp) {
            if (cr.getName().matches(lv.getLabel()) && (cr.getId().toString().matches(lv.getValue()))) {
                return lv;
            }
        }
        return null;
    }

    public boolean validationPasses() {
        boolean vp = true;
        if (catalog.getName() == null || catalog.getName().matches("")) {
            errorMessage("Required field, name.");
            vp = false;
        }
        if (catalog.getNameEs() == null || catalog.getNameEs().matches("")) {
            errorMessage("Required field, name (es).");
            vp = false;
        }

        if (catalog.getCode() == null || catalog.getCode().matches("")) {
            errorMessage("Required field, name.");
            vp = false;
        }

        System.out.println("val:" + vp);
        return vp;
    }

    public String save() throws Exception {
        if (cancel != null) {
            return CANCEL;
        }

        if (delete != null) {
            return delete();
        }

        boolean isNew = (catalog.getId() == null);

        //errorMessage("El distintivo ya existe.");
        List<Category> old = catalog.getCategorys();
        catalog.setCategorys(new ArrayList<Category>());
        System.out.println("categoryoptionspicked: " + categoryOptionsPicked);
        if (categoryOptionsPicked != null) {
            for (String s : getCategoryOptionsPicked()) {
                Category p = categoryManager.get(new Long(Integer.parseInt(s)));
                catalog.getCategorys().add(p);

            }
        }
        System.out.println("categorys: " + catalog.getCategorys().size());
        if (validationPasses()) {
            if (catalog.isActual()) {
                for (Catalog c : catalogManager.getAll()) {
                    if (c.isActual()) {
                        c.setActual(false);
                        c = catalogManager.save(c);
                    }
                }
            }
            catalog = catalogManager.save(catalog);
            /*if (isNew) {
                for (Category c : catalog.getCategorys()) {
                    c.getCatalogs().add(catalog);
                    categoryManager.save(c);
                }
            } else {
                for (Category c:catalog.getCategorys()){
                    if (!old.contains(c)){
                        c.getCatalogs().add(catalog);
                    }
                    categoryManager.save(c);
                }
            }*/
        } else {
            edit();
            return ERROR;
        }

        String key = (isNew) ? "catalog.added" : "catalog.updated";
        saveMessage(getText(key));

        if (!isNew) {
            return INPUT;
        } else {
            return SUCCESS;
        }
    }

    public void setCatalogManager(CatalogManager catalogManager) {
        this.catalogManager = catalogManager;
    }

    public void setProductManager(GenericManager<Product, Long> productManager) {
        this.productManager = productManager;
    }

    public void setCategoryManager(GenericManager<Category, Long> categoryManager) {
        this.categoryManager = categoryManager;
    }

    public List getCatalogs() {
        return catalogs;
    }

    public String list() {
        catalogs = catalogManager.getAll();
        return SUCCESS;
    }

    public String helpList() {
        return SUCCESS;
    }

    public String help() {
        return SUCCESS;
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    public String saveCatalogDescription() {
        System.out.println("NAME: " + name);
        System.out.println("id: " + getRequest().getAttribute("id"));
        System.out.println("d: " + getRequest().getAttribute("catalog"));

        return SUCCESS;
    }
    private String catalogName;
    private String catalogDescription;

    /**
     * @return the catalogName
     */
    public String getCatalogName() {
        return catalogName;
    }

    /**
     * @param catalogName the catalogName to set
     */
    public void setCatalogName(String catalogName) {
        this.catalogName = catalogName;
    }

    /**
     * @return the catalogDescription
     */
    public String getCatalogDescription() {
        return catalogDescription;
    }

    /**
     * @param catalogDescription the catalogDescription to set
     */
    public void setCatalogDescription(String catalogDescription) {
        this.catalogDescription = catalogDescription;
    }

    /**
     * @return the categoryOpions
     */
    public Set<String> getCategoryOpions() {
        return categoryOpions;
    }

    /**
     * @param categoryOpions the categoryOpions to set
     */
    public void setCategoryOpions(Set<String> categoryOpions) {
        this.categoryOpions = categoryOpions;
    }

    /**
     * @return the categoryOptionsPicked
     */
    public Set<String> getCategoryOptionsPicked() {
        return categoryOptionsPicked;
    }

    /**
     * @param categoryOptionsPicked the categoryOptionsPicked to set
     */
    public void setCategoryOptionsPicked(Set<String> categoryOptionsPicked) {
        this.categoryOptionsPicked = categoryOptionsPicked;
    }

    /**
     * @return the categoryListLeft
     */
    public List<LabelValue> getCategoryListLeft() {
        return categoryListLeft;
    }

    /**
     * @param categoryListLeft the categoryListLeft to set
     */
    public void setCategoryListLeft(List<LabelValue> categoryListLeft) {
        this.categoryListLeft = categoryListLeft;
    }

    /**
     * @return the categoryListRight
     */
    public List<LabelValue> getCategoryListRight() {
        return categoryListRight;
    }

    /**
     * @param categoryListRight the categoryListRight to set
     */
    public void setCategoryListRight(List<LabelValue> categoryListRight) {
        this.categoryListRight = categoryListRight;
    }
    /**
     * @return the actual
     */
}